package com.pickgliss.toplevel
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.utils.Dictionary;
	import flash.utils.setInterval;
	

	public final class StageReferance
	{
		private static var _stageHeight:int
		private static var _stageWidth:int
		private static var _stage:Stage;
		private static const eventDispatcher : EventDispatcher = new EventDispatcher();
		
		private static const widthChange : String  = "widthChange";
		private static const heightChange : String  = "heightChange";
		private static var _dict : Object = new Object();
		
		public static function get stageWidth():int
		{
			return _stageWidth;
		}
		
		public static function set stageWidth(value:int):void
		{
			if(_stageWidth == value)return;
			_stageWidth = value;
			_dict[widthChange] = true;
		}

		public static function get stageHeight():int
		{
			return _stageHeight;
		}

		public static function set stageHeight(value:int):void
		{
			if(_stageHeight == value)return;
			_stageHeight = value;
			_dict[heightChange] = true;
		}
		public static function commit() : void
		{
			if(_dict[widthChange] || _dict[heightChange])
			{
				eventDispatcher.dispatchEvent(new Event(Event.RESIZE));
				_dict[widthChange] = false;
				_dict[heightChange] = false;
			}
			
		}

		/**
		 * 
		 * @param $stage 舞台对象
		 * 初始化 舞台引用
		 * 
		 */		
		public static function setup($stage:Stage):void
		{
			if(_stage != null)return;
			_stage = $stage;
			_stage.addEventListener(Event.EXIT_FRAME,__onNextFrame);
			_stage.addEventListener(Event.RESIZE,__onResize,false,100);
			_stage.stageFocusRect = false;
			__onResize(null)
		}
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			eventDispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		public static function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			eventDispatcher.removeEventListener(type,listener,useCapture);
		}
		
		private static function __onNextFrame(event:Event):void
		{
			if(_stage.stageWidth > 0 )
			{
				_stage.removeEventListener(Event.EXIT_FRAME,__onNextFrame);
				stageWidth = _stage.stageWidth;
				stageHeight = _stage.stageHeight;
				commit();
				eventDispatcher.dispatchEvent(new Event(Event.RESIZE));
			}
		}
		
		private static function __onResize(event:Event):void
		{
			stageWidth = _stage.stageWidth;
			stageHeight = _stage.stageHeight;
			commit();
			_stage.addEventListener(Event.EXIT_FRAME,__onNextFrame);
		}
		
		public static function get gameStageWidth() : int
		{
			var sWidth : int = (stageWidth < 800 ? 800 : stageWidth);
			sWidth = (stageWidth > 1500 ? 1500 : stageWidth);
			return sWidth;
		}
		public static function get gameStageHeight() : int
		{
			var sHeight : int = (stageHeight < 480 ? 480 : stageHeight);
			sHeight = (stageHeight > 900 ? 900 : sHeight);
			return sHeight;
		}
		public static function get gameScaleX() : Number
		{
			if(gameStageWidth < 1000)return (gameStageWidth / 1000);
			return 1;
		}
		public static function get gameScaleY() : Number
		{
			if(gameStageHeight < 600)return (gameStageHeight / 600);
			return 1;
		}
		
		/**
		 * 
		 * @return 获取舞台
		 * 
		 */		
		public static function get stage():Stage
		{
			return _stage;
		}

		public function StageReferance()
		{
		}
	}
}